#!/usr/bin/env python
# encoding: utf-8
"""
Utility.py

Copyright (C) 2009  Thorsten Philipp

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""

import sys
import os
import unittest
import tempfile
from subprocess import *


class Cmd(object):
  """Run a binary"""
  def __init__(self,command,args=""):

    super(Cmd, self).__init__()
    
    self.command = command
    self.arguments = args
        
    self.isError = 0
    self.error = []
    self.response = []
    
      
    
  
  def run(self):
    """Execute the command with all it's arguments."""
    cmd = self.command + " " + self.arguments
    process = Popen(cmd,stdout=PIPE,stderr=PIPE,shell="true")
    stdout, stderr = process.communicate()
    self.response = stdout.splitlines()
    self.error = stderr.splitlines()
    
    if len(self.error) > 0:
      self.isError = 1
      
    
  def response_asString(self):
    """return the response as a string (instead of line by line)"""
    return("\n".join(self.response))
    
def filetemp():
  (fd, fname) = tempfile.mkstemp()
  return (os.fdopen(fd, "w+b"), fname)

class UtilityTests(unittest.TestCase):
  def setUp(self):
    pass


if __name__ == '__main__':
  unittest.main()